package com.mobibom.pipelines;

import pipelines.free.beta.R;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SettingsActivity extends Activity {
	CheckBox vibrateCheck, gridCheck;
	 private AdView adView1;
	 private static final String MY_BANNER_UNIT_ID = "a14ed7bde839b79";
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.settingsview);
	    

	    
	    setUpView();
	    setClickListeners();
	}
	 private void setClickListeners() {
		vibrateCheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		    	SharedPreferences settings = getSharedPreferences("levelsInDB", Context.MODE_PRIVATE);
		    	Editor editor = settings.edit();
		    	
		    	editor.putBoolean("vibrate", isChecked);
		        editor.commit();
		    }
		});
		gridCheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		    	SharedPreferences settings = getSharedPreferences("levelsInDB", Context.MODE_PRIVATE);
		    	Editor editor = settings.edit();
		    	
		    	editor.putBoolean("grid", isChecked);
		        editor.commit();
		    }
		});
	}

	private void setUpView() {
		 vibrateCheck =		(CheckBox)findViewById(R.id.checkBoxVibrate);
		 gridCheck = 		(CheckBox)findViewById(R.id.checkBoxGrid);
		 
		    
	    SharedPreferences settings = getSharedPreferences("levelsInDB", Context.MODE_PRIVATE);
	    
	    boolean vibrate = settings.getBoolean("vibrate", true);
	    boolean grid = settings.getBoolean("grid", true);

	    vibrateCheck.setChecked(vibrate);
	    gridCheck.setChecked(grid);
	    
	    // Lookup R.layout.main
        LinearLayout layout = (LinearLayout)findViewById(R.id.layoutAdsSettings);
        // Create the adView
        // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
        adView1 = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
        // Add the adView to it
        layout.addView(adView1);
        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        //request.setTesting(true);
        adView1.loadAd(request);     
	}

	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        	Intent start = new Intent(getApplicationContext(), StartScreenActivity.class);
    		startActivity(start);
    		this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
